<template>
  <div id="app">
    <div class="preview">
      <img ref='previewImg' src="./assets/image/preview.png">
    </div>
    <label for="fileInput"></label>
    <input type="file" accept="image/*" style="display:none" id="fileInput" ref="fileInput" @change="handleChange" />
    <!-- 查询失败 -->
    <div class="mask" v-show="errored" @click.self="errored = false">
      <div class="error">
        <button class="btn" @click="$refs.fileInput.click()">重新拍照</button>
      </div>
    </div>
    <!-- 查询结果 -->
    <div class="result" v-show="succeed.code">
      <img src="./assets/image/logo.png">
      <div class="content">
        <h4>尊敬的用户您好：</h4>
        <p>你所查询的数码为：<b>{{succeed.code}}</b></p>
        <p v-if="succeed.status==='1'">感谢您购买正品产品，谢谢使用</p>
        <template v-else-if="succeed.status==='0'">
          <p>此数码不存在，谨防假冒，或核对后重新查询，并请拨打咨询热线。</p>
          <p>咨询热线：<a href="tel:000-20095599">000-20095599</a></p>
          <p>感谢您的使用！</p>
        </template>
      </div>
      <button class="btn" @click="$refs.fileInput.click()">继续查询</button>
    </div>
    <!-- 进度条 -->
    <div class="mask" v-show="loading">
      <ProgressBar :percent="percent" :show-text="true"></ProgressBar>
    </div>

  </div>
</template>

<script>
import Lrz from 'lrz'
import upload from './utils/upload'
import ProgressBar from './components/ProgressBar'
export default {
  components: { ProgressBar },
  data() {
    return {
      percent: 0,
      loading: false,
      errored: false,
      succeed: {
        status: '',
        code: '',
      },
    }
  },
  methods: {
    handleChange(e) {
      Lrz(e.target.files[0], { width: 300 })
        .then(({ base64, formData }) => {
          this.percent = 0
          this.loading = true
          this.errored = false
          this.succeed.code = ''
          this.$refs.previewImg.src = base64
          formData.append('base64img', base64)
          formData.append('testStatus', '1')
          this.uploadImg(formData)
        })
    },
    uploadImg(body) {
      upload('/Antifake/GetCameraFile', {
        body,
        success: (res) => {
          this.loading = false
          this.percent = 0
          const { Data: { Status, Code } } = JSON.parse(res)
          if (Status === '0' || Status === '1') {
            this.succeed.code = Code
            this.succeed.status = Status
          } else {
            this.errored = true
            this.succeed.code = ''
          }
        },
        fail: console.log,
        onprogress: ({ loaded, total }) => {
          const p = ~~((loaded / total) * 100)
          this.percent = p
        },
        onerror: () => {
          this.percent = 0
        },
        onload: () => {
          this.percent = 99
        },
      })
    },
  }
}
</script>

<style lang="less">
@import url("./utils/vars.less");
html,
body,
#app {
  margin: 0;
  height: 100%;
}
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: url("./assets/image/background.jpg") no-repeat center/100% 100%;
  .mask {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(51, 51, 51, 0.589);
    backdrop-filter: blur(4px);
    .error {
      position: relative;
      border-radius: 10px;
      width: 300px;
      height: 350px;
      background: url("./assets/image/error.jpg") no-repeat center / cover;
      button {
        position: absolute;
        left: 50%;
        bottom: 30px;
        margin-left: -120px;
        color: @blue;
      }
    }
  }
  button.btn {
    width: 240px;
    height: 46px;
    line-height: 46px;
    border-radius: 46px;
    font-size: 20px;
    font-weight: bold;
    text-align: center;
    background-color: @yellow;
    border: 0;
    outline: none;
  }
  .preview {
    padding: 20px;
    border: 2px solid white;
    background-color: rgba(100, 65, 82, 0.562);
    backdrop-filter: blur(3px);
    img {
      width: 166px;
      border-radius: 6px;
    }
  }
  label[for="fileInput"] {
    display: inline-block;
    // position: fixed;
    width: 82px;
    height: 68px;
    margin-top: 30px;
    // bottom: 60px;
    // left: 50%;
    // margin-left: -41px;
    background: url("./assets/image/label.png") no-repeat 0 0/cover;
  }
  .result {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: url("./assets/image/result.jpg") no-repeat center / 100% 100%;
    img {
      position: absolute;
      left: 50%;
      top: 80px;
      width: 106px;
      height: 106px;
      margin-left: -53px;
      @media screen and (max-width: 320px) {
        width: 80px;
        height: 80px;
        margin-left: -40px;
        top: 74px;
      }
    }
    .content {
      position: absolute;
      top: 200px;
      left: 50%;
      width: 300px;
      color: #122c56;
      margin-left: -150px;
      @media screen and (max-width: 320px) {
        width: 260px;
        margin-left: -130px;
      }
      h4 {
        margin: 0;
      }
      p {
        margin: 0;
        // text-indent: 2em;
      }
      a,
      a:visited,
      a:active {
        color: @yellow;
        font-weight: bold;
        text-decoration: none;
      }
    }
    button {
      position: absolute;
      bottom: 40px;
      left: 50%;
      // width: 240px;
      margin-left: -120px;
      color: #fff;
      box-shadow: 0 0 17px 4px #c6d5d6;
    }
  }
}
</style>
